package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.TPhysicalexamrecord;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.TPhysicalexamrecordService;
import com.itheima.health.utils.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("tPhyexamrecord")
@Slf4j
@CrossOrigin
public class TPhyexamrecordController {

    @DubboReference
    private TPhysicalexamrecordService tPhysicalexamrecordService;
    @Autowired
    private QiniuUtils qiniuUtils;
    @Autowired
    private RedisTemplate redisTemplate;
    @DubboReference
    private MemberService memberService;

    @GetMapping("findpage")
    public Result finaPage(QueryPageBean queryPageBean) {
        PageResult page = tPhysicalexamrecordService.findPage(queryPageBean);
        log.info("tPhyexamrecord:findpage.data{}",page);
        return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, page);
    }

    @PostMapping("add")
    public Result add(@RequestBody TPhysicalexamrecord tPhysicalexamrecord) {
//        String filename = tPhysicalexamrecord.getReport().replace(qiniuUtils.getUrlPrefix(), "");
//        tPhysicalexamrecord.setReport(filename);
        tPhysicalexamrecordService.add(tPhysicalexamrecord,Order.ORDERREPORT_YES);
        redisTemplate.boundSetOps(RedisConst.PIC_REDIS_PRE).remove(tPhysicalexamrecord.getReport());
        return new Result(true, MessageConst.ADD_CHECKITEM_SUCCESS);
    }

    @PostMapping("delete")
    public Result delete(Integer id) {
        tPhysicalexamrecordService.deleteById(id);
        return new Result(true, MessageConst.DELETE_CHECKITEM_SUCCESS);
    }

    @GetMapping("findById")
    public Result findById(Integer id) {
        TPhysicalexamrecord tPhysicalexamrecord=tPhysicalexamrecordService.findById(id);
        return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, tPhysicalexamrecord);
    }

    @PostMapping("edit")
    public Result edit(@RequestBody TPhysicalexamrecord tPhysicalexamrecord) {
        tPhysicalexamrecordService.edit(tPhysicalexamrecord);
        return new Result(true, MessageConst.EDIT_CHECKITEM_SUCCESS);
    }

    @GetMapping("findAll")
    public Result findAll() {
        List<TPhysicalexamrecord> all = tPhysicalexamrecordService.findAll();
        return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, all);
    }

    @GetMapping("findByStatus")
    public Result findAll(String status) {
        List<TPhysicalexamrecord> all = tPhysicalexamrecordService.findAll(status) ;
        return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, all);
    }

    @PostMapping("upload")
    public Result upload(@RequestParam("imgFile") MultipartFile file) {
        //获取文件原始名称
        String originalFilename = file.getOriginalFilename();
        //自己构造文件名称
        String fileName = System.currentTimeMillis() + "_" +  originalFilename;
        //将图片存入redis
        redisTemplate.boundSetOps(RedisConst.REPORT_REDIS_PRE).add(fileName);
        try {
            qiniuUtils.upload2Qiniu(file.getInputStream(), fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConst.PIC_UPLOAD_FAIL);
        }
        String imgUrl = qiniuUtils.getUrlPrefix() + fileName;
        return new Result(true, MessageConst.PIC_UPLOAD_SUCCESS, imgUrl);
    }

    @GetMapping("findTphyexamrecordList")
    public Result findTphyexamrecordList(String telephone){
        Member member = memberService.findByTele(telephone);
        Integer memberId = member.getId();
        List<TPhysicalexamrecord> tPhysicalexamrecord=tPhysicalexamrecordService.findByMemberId(memberId);

        return new Result(true,MessageConst.ACTION_SUCCESS,tPhysicalexamrecord);
    }
}
